//
//  MagicImageView.m
//  BigImageDemo
//
//  Created by tym on 2017/7/20.
//  Copyright © 2017年 tym. All rights reserved.
//

#import "MagicImageView.h"
#import "UIImageView+YPImage.h"
#import "UIImageView+WebCache.h"
@interface MagicImageView ()
{
    BOOL isMe;
}
@property (strong, nonatomic) UIView *backView;
@property (strong, nonatomic) UIImageView *actionImageView;

@end

@implementation MagicImageView

-(void)awakeFromNib{
    [super awakeFromNib];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picTapAction:)]];
}

- (instancetype)initWithImage:(UIImage *)image isMe:(BOOL)me{
    self = [super initWithImage:image];
    isMe = me;
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picTapAction:)]];
    return self;
}

-(void)yp_setImageUlr:(NSString *)imageUrl placeHolder:(NSString *)placeHolder{
    [super yp_setImageUlr:imageUrl placeHolder:placeHolder];
    
    [_actionImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
                               completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        [UIView animateWithDuration:.3 animations:^{
//            CGFloat fixelW = CGImageGetWidth(_actionImageView.image.CGImage);
//            CGFloat fixelH = CGImageGetHeight(_actionImageView.image.CGImage);
//            _actionImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixelH * [UIScreen mainScreen].bounds.size.width / fixelW);
//            float BtnW = 121;
//            float BtnH = 36;
//            _actionImageView.center = _backView.center;
//
//        }];
    }];
}

- (void)picTapAction:(UIGestureRecognizer *)gr{
    if (self.image==nil) {
        return;
    }
    self.hidden = YES;
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    _backView.backgroundColor = [UIColor blackColor];
    [_backView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapAction)]];
    [[UIApplication sharedApplication].delegate.window addSubview:_backView];
    
    _actionImageView = [[UIImageView alloc] initWithImage:self.image];
    _actionImageView.contentMode = UIViewContentModeScaleAspectFill;
    _actionImageView.frame = self.frame;
    [[UIApplication sharedApplication].delegate.window addSubview:_actionImageView];
   
    [UIView animateWithDuration:.3 animations:^{
        
        CGFloat fixelW = CGImageGetWidth(_actionImageView.image.CGImage);
        CGFloat fixelH = CGImageGetHeight(self->_actionImageView.image.CGImage);
        _actionImageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, fixelH * [UIScreen mainScreen].bounds.size.width / fixelW);
        
        float BtnW = 121;
        float BtnH = 36;
        _actionImageView.center = _backView.center;
    }];
}

- (void)backTapAction{
    
    [UIView animateWithDuration:.2 animations:^{
        float BtnW = 121;
        float BtnH = 36;
        _actionImageView.frame = self.frame;
        self->_backView.alpha = .3;
    } completion:^(BOOL finished) {
        [self->_backView removeFromSuperview];
        [self->_actionImageView removeFromSuperview];
        self.hidden = NO;
    }];
}

-(void)changeAction:(id)sender{
    
    if (self.changeImageBlock) {
        self.changeImageBlock();
    }
}
@end
